package com.shwread.android.qysw.fragment;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ciko.code.bean.MyBusinessTripBean;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.http.Req.QryWorkOutsideInfoList;
import com.ciko.code.views.pullrefresh.PullToRefreshBase;
import com.ciko.code.views.pullrefresh.PullToRefreshListView;
import com.mywork.R;
import com.shwread.android.qysw.adapter.BusinessTripAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 发出因公外出申请页面
 */
public class MyOutFragment extends Fragment implements HttpResponseListener {

    private PullToRefreshListView lVMyFieldWork;
    private ProgressDialog progressDialog;
    private List<MyBusinessTripBean> myFieldWorkBeanList;
    private List<MyBusinessTripBean> myFieldWorkBeanList1;

    private View view;
    private String title;
    private final int LENGTH = 10;
    private final int isFresh = 0;//0代表刷新，
    private final int isLoad = 1;//1代表加载
    private int refreshLoadTag = -1;//刷新还是加载的标志

    private BusinessTripAdapter businessTripAdapter;
    private TextView empty;
    private boolean hasMore = false;

    private int page = 1;
    private boolean isVisible = false;


    public MyOutFragment(String title) {
        this.title = title;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e("Tag","onCreateView");
        view = inflater.inflate(R.layout.fragment_my_out, null);
        initView();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.e("Tag","onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        Log.e("Tag","onResume");
        super.onResume();
        doHttpRequst(0, page, LENGTH);
    }

    @Override
    public void onAttach(Activity activity) {
        Log.e("Tag","onAttach");
        super.onAttach(activity);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        Log.e("Tag","onHiddenChanged");
        super.onHiddenChanged(hidden);
    }

    private void initView() {

        myFieldWorkBeanList1 = new ArrayList<MyBusinessTripBean>();
        lVMyFieldWork = (PullToRefreshListView) view.findViewById(R.id.lVMyFieldWork);
        empty = (TextView) view.findViewById(R.id.empty);
        lVMyFieldWork.setPullLoadEnabled(true);
        lVMyFieldWork.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                refresh();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadMore();
            }
        });


    }


    @Override
    public void onStart() {
        Log.e("Tag","onStart");
        super.onStart();
    }

    @Override
    public void onRequestStart(String tag) {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("加载中...");
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    private void loadMore() {
        refreshLoadTag = 1;
        if (hasMore) {
            page = page + 1;
            doHttpRequst(0, page, LENGTH);
        } else {
            lVMyFieldWork.onPullUpRefreshComplete();
            lVMyFieldWork.onPullDownRefreshComplete();
        }
    }

    private void refresh() {
        page = 1;
        refreshLoadTag = 0;
        doHttpRequst(0, page, LENGTH);
    }

    private void doHttpRequst(int type, int page, int length) {
        HttpClient.getInstance(getActivity()).post(new QryWorkOutsideInfoList(0, page, LENGTH), this);
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        lVMyFieldWork.onPullUpRefreshComplete();
        lVMyFieldWork.onPullDownRefreshComplete();
        myFieldWorkBeanList = (List<MyBusinessTripBean>) response.getReturnObject();
        if (refreshLoadTag == -1 && myFieldWorkBeanList.size() == 0) {
            lVMyFieldWork.getRefreshableView().setEmptyView(empty);
            return;
        }
//        //没有更多数据
        if (myFieldWorkBeanList.size() < 10) {
            lVMyFieldWork.setHasMoreData(false);
            hasMore = false;
        } else {
            hasMore = true;
        }
        if (refreshLoadTag == isLoad) {
            myFieldWorkBeanList1.addAll(myFieldWorkBeanList);
            businessTripAdapter.notifyDataSetChanged();
        } else {
            myFieldWorkBeanList1.clear();
            myFieldWorkBeanList1.addAll(myFieldWorkBeanList);
            businessTripAdapter = new BusinessTripAdapter(getActivity(), myFieldWorkBeanList1, 1);
            lVMyFieldWork.getRefreshableView().setAdapter(businessTripAdapter);

        }

    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
            Toast.makeText(getActivity(), "加载失败，请稍后重试", Toast.LENGTH_SHORT).show();
        }
    }
}
